"ui";
storage = storages.create("setting");
storage.put("color", "#ff0000");

function mainui() {
    ui.layout(
        <frame>
            <scroll>
                <vertical>
                    <vertical bg="file://res/卒业.png">
                        <card bg="#33000000">
                            <vertical>
                                <text text="从网页下载的图片:" textColor="#FFFFFF"/>
                                <input id="inurl" textStyle="bold" text="请复制粘贴网络图片链接(长按清除文本)" textColor="#FFFFFF"/>
                                <button text="下载" id="downloadimg" backgroundTint="#00000000" textColor="#FFFFFF"/>
                                <text text="从本地上传的图片:" textColor="#FFFFFF"/>
                                <input id="upurl" textStyle="bold" text="请复制粘贴本地图片链接(长按清除文本)" textColor="#FFFFFF"/>
                                <button text="上传" id="uploadimg" backgroundTint="#00000000" textColor="#FFFFFF"/>
                                <text  text="当前选择修改后照片的背景色:" textColor="#FFFFFF"/>
                                <text id="targetcolor" w="90" h="150" bg="{{ '#99'+storage.get('color').substr(1) }}" layout_gravity="center"/>
                                <text text="背景色选择:" textColor="#FFFFFF"/>
                                <scroll>
                                    <horizontal>
                                        <vertical id="纯黑" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="纯黑" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#99000000" />
                                        </vertical>
                                        <vertical id="骚粉" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="骚粉" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#99FFB6C1" />
                                        </vertical>
                                        <vertical id="原谅" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="原谅" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#993CB371" />
                                        </vertical>
                                        <vertical id="真爱" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="真爱" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#9900BFFF" />
                                        </vertical>
                                        <vertical id="基佬" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="基佬" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#999400D3" />
                                        </vertical>
                                        <vertical id="白云" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="白云" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#99FFFFFF" />
                                        </vertical>
                                        <vertical id="残阳" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                            <text text="残阳" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                            <text w="50" h="50" bg="#99FF0000" />
                                        </vertical>
                                    </horizontal>
                                </scroll>
                                <horizontal>
                                    <vertical id="樱花" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="樱花" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99feeeed" />
                                    </vertical>
                                    <vertical id="深海" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="深海" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#990066ff" />
                                    </vertical>
                                    <vertical id="橙子" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="橙子" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99ff6600" />
                                    </vertical>
                                    <vertical id="月光" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="月光" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99ffff00" />
                                    </vertical>
                                    <vertical id="淡然" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="淡然" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99ccffff" />
                                    </vertical>
                                    <vertical id="咖啡" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="咖啡" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99cc6600" />
                                    </vertical>
                                    <vertical id="雨后" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="雨后" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99c0ffee" />
                                    </vertical>
                                </horizontal>
                                <input id="selfcolor" textStyle="bold" text="请输入自定义16进制颜色码(长按清除文本)" textColor="#FFFFFF"/>
                                <horizontal gravity="center">
                                    <button text="提交颜色码" id="commitcolor" backgroundTint="#00000000" textColor="#FFFFFF"/>
                                    <button text="查看颜色码" id="viewcolor" backgroundTint="#00000000" textColor="#FFFFFF"/>
                                </horizontal>
                            </vertical>
                        </card>
                    </vertical>
                    <card bg="#33000000">
                        <vertical>
                            <text text="当前要处理的图片:" textColor="#FFFFFF"/>
                            <img id="imgbg" src="file://res/1.png" />
                            <text text="图片处理较慢，请耐心等待!"/>
                            <button text="处理图片" id="proimg" />
                        </vertical>
                    </card>
                    <vertical layout_gravity="bottom">
                        <text text="以下内容为加载网页:https://www.cnblogs.com/planetwithpig/p/11663209.html"/>
                        <webview id="webview" w="auto" h="*" />
                    </vertical>
                </vertical>
            </scroll>
        </frame>
    )
    ui.webview.loadUrl("https://www.cnblogs.com/planetwithpig/p/11663209.html")
    ui.statusBarColor("#000000");
    ui.proimg.on("click", () => {
        img = images.read("./res/1.png") //读图
        pointcolor = "#" + (colors.toString(images.pixel(img, img.getWidth() / 10, img.getHeight() / 10))).substr(3);
        bimg = images.toBytes(img); //将图片转换为字节数组
        bmimg = android.graphics.BitmapFactory.decodeByteArray(bimg, 0, bimg.length); //字节数组转bitmat
        desbmimg = bmimg.copy(android.graphics.Bitmap.Config.ARGB_8888, true); //解决E: Wrapped java.lang.IllegalStateException
        //二值化图像
        dimg = images.interval(img, pointcolor, 50)
        images.save(dimg, "./res/2.png") //保存图
        bdimg = images.toBytes(dimg); //将图片转换为字节数组
        bmdimg = android.graphics.BitmapFactory.decodeByteArray(bdimg, 0, bdimg.length); //字节数组转bitmat
        desbmdimg = bmdimg.copy(android.graphics.Bitmap.Config.ARGB_8888, true); //解决E: Wrapped java.lang.IllegalStateException
        //图像处理
        bmh = bmdimg.getHeight();
        bmw = bmdimg.getWidth();
        //遍历图片
        for (i = 0; i < bmw; i++) {
            for (j = 0; j < bmh; j++) {
                if (desbmdimg.getPixel(i, j) == desbmdimg.getPixel(bmw / 10, bmh / 10)) {
                    desbmimg.setPixel(i, j, colors.parseColor(storage.get("color")));
                }
            }
        }
        outputSteam = java.io.ByteArrayOutputStream();
        desbmimg.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputSteam);
        newbyte = outputSteam.toByteArray();
        newimg = images.fromBytes(newbyte);
        images.save(newimg, "./res/3.png");
        outputSteam.close();
        imgbg();
    });
    ui.真爱.on("click", () => {
        toast("如果真爱有颜色，那一定是蓝色！")
        storage.put("color", "#00BFFF");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.纯黑.on("click", () => {
        storage.put("color", "#000000");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.骚粉.on("click", () => {
        storage.put("color", "#FFB6C1");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.原谅.on("click", () => {
        storage.put("color", "#3CB371");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.真爱.on("click", () => {
        storage.put("color", "#00BFFF");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.基佬.on("click", () => {
        storage.put("color", "#9400D3");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.白云.on("click", () => {
        storage.put("color", "#FFFFFF");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.残阳.on("click", () => {
        storage.put("color", "#FF0000");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.樱花.on("click", () => {
        storage.put("color", "#feeeed");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.深海.on("click", () => {
        storage.put("color", "#0066ff");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.橙子.on("click", () => {
        storage.put("color", "#ff6600");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.月光.on("click", () => {
        storage.put("color", "#ffff00");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.淡然.on("click", () => {
        storage.put("color", "#ccffff");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.咖啡.on("click", () => {
        storage.put("color", "#cc6600");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.雨后.on("click", () => {
        storage.put("color", "#c0ffee");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.inurl.on("long_click", () => {
        ui.inurl.setText("");
    });
    ui.selfcolor.on("long_click", () => {
        ui.selfcolor.setText("#");
    });
    ui.commitcolor.on("click", () => {
        storage.put("color", ui.selfcolor.getText().toString());
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.upurl.on("long_click", () => {
        ui.upurl.setText("");
    });
    ui.imgbg.on("click", () => {
        ui.imgbg.setSource("file://res/1.png");
    });
    ui.downloadimg.on("click", () => {
        url = ui.inurl.getText().toString();
        threads.start(function() {
            var res = http.get(url);
            if (res.statusCode != 200) {
                toast("请求失败");
            }
            files.writeBytes("./res/1.png", res.body.bytes());
            toast("下载成功");
            className("android.widget.ImageView").findOne().click();
        })
    });
    ui.uploadimg.on("click", () => {
        url = ui.upurl.getText().toString();
        threads.start(function() {
            img = images.read(url);
            images.save(img, "./res/1.png")
            toast("上传成功");
            className("android.widget.ImageView").findOne().click();
        })
    });
}

function imgbg() {
    ui.layout(
        <frame>
            <ScrollView>
                <vertical>
                    <text text="修改前的图片：" />
                    <img src="file://res/1.png" />
                    <text text="二值化的图片：" />
                    <img src="file://res/2.png" />
                    <text text="修改后的图片" />
                    <img src="file://res/3.png" />
                </vertical>
            </ScrollView>
        </frame>
    );

}
mainui();